package com.wbsxch.blog.controller;

import com.wbsxch.blog.result.EditormdResult;
import com.wbsxch.blog.utils.OSSClientUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@RestController
public class UploadController {

    @PostMapping("/upload_img")
    public EditormdResult upload(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file){
        EditormdResult editormdResult = new EditormdResult();
        try {
            String name = file.getOriginalFilename();
            String objectName = OSSClientUtils.IMAGESURL+name;
            InputStream inputStream = file.getInputStream();
            String imageUrl = OSSClientUtils.uploadImage(objectName, inputStream);
            editormdResult.setSuccess(1);
            editormdResult.setMessage("上传成功");
            editormdResult.setUrl(imageUrl);
        } catch (Exception e) {
            e.printStackTrace();
            editormdResult.setSuccess(0);
            editormdResult.setMessage("上传失败");
        }
        return editormdResult;
    }
}
